/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
// -*- coding:iso-8859-15 mode: c++ -*-
/** 
 * \file
 * \brief MaCI_Energy Logger implementation.
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 *
 * $Id: EnergyLogger.cpp,v 1.3 2009-06-12 12:49:23 morsko Exp $
 *
 */
#include "EnergyLogger.hpp"
#include "gimutils.h"

EnergyLogger::EnergyLogger(MaCI::Energy::CEnergyClient *aClient,
                           std::string aMaCISLName):
  CSync(1, 1),     
  Logger(),
  iEClient(aClient),
  iRunning(false),
  iRunningThreadStopped(true),
  iFile(NULL),
  iMaCISLName(aMaCISLName),
  iAdditionalInformation("Not active")
  
{
  iEClient->SetEnergyClientCallback(this);
}
//*****************************************************************************

EnergyLogger::~EnergyLogger(){
  if(iRunning){
    StopLogging();
  }
  delete (iEClient);

}
//*****************************************************************************
 
bool EnergyLogger::StartLogging(void)
{
  dPrint(10,"Start called");
  
  if(!iRunning){

    char buffer[256];
    dPrint(5,"Start logging Energyn events");
    if(iFilenameSet){
  
      sprintf(buffer,"data/%s.txt",
             iFilename.c_str());
      
    }else{
    
      gim::time tstamp(true);
      tm t;
      //      MaCI::Common::TTimestamp tstamp;
      tstamp.getLocalTime(t);
       
      sprintf(buffer,"data/%s_%.2d_%.2d_%d_%.2d:%.2d:%.2d.txt",
              iMaCISLName.c_str(),
              t.tm_mday,
              t.tm_mon+1,
              t.tm_year+1900,
              t.tm_hour,
              t.tm_min,
              t.tm_sec);
              
     
    }

    
    iFile = fopen(buffer,"wt");

      /////////Search the group and instance names :)
    std::string instance;
    std::string group;

    MaCI::MaCICtrl::SMaCISL sl(iMaCISLName);
    if (sl.IsValidFQMaCISL()) {
      group = sl.GetDeviceGroup();
      instance = sl.GetInstance();
    }
    //////////

    fprintf(iFile,"%%MaCI_Energy log file\n");
    fprintf(iFile,"%%Group: %s Instance: %s\n",group.c_str(),instance.c_str());

    fprintf(iFile,"%%timestamp_sec timestamp_usec energyLevel(%%) energyMax consumption\n");
    fprintf(iFile,"\n");
  
  
  
    iRunning = true; 
    
  }

  return false;
}
//*****************************************************************************
 
bool EnergyLogger::StopLogging(void)
{

  dPrint(10,"stop called");
  if(iRunning){
  
    iRunning = false;
  
    fclose(iFile);
    Lock();
    iAdditionalInformation = "Not active";
    Unlock();
    
  }
  return false;
}
//*****************************************************************************

void EnergyLogger::OnEnergyDataEvent(const MaCI::Energy::CEnergyData &aData, 
                                     const unsigned int aDataSequence,
                                     MaCI::Energy::CEnergyClient &aEnergyClient,
                                     void * aUserPtr)
{
  using namespace MaCI::Energy;
  if(iRunning){
    Lock();
    char buffer[256];
    const MaCI::Common::TTimestamp *t = aData.GetTimestamp();
    if(t){
      fprintf(iFile,"%u %u ", t->timestamp_s,t->timestamp_us);
      sprintf(buffer,"%u %u ", t->timestamp_s,t->timestamp_us);
    }else{
      fprintf(iFile,"0 0 ");
      sprintf(buffer,"0 0 ");
    }
    const TEnergyEvent *ee = aData.GetEnergyEvent();
    if(ee &&ee->type == MaCI::Energy::KEventTypeEnergyData){
      fprintf(iFile,"%f %f %f\n",ee->energyLevel, ee->energyMax,ee->consumption);
      sprintf(buffer,"level %.2f %%, max %.2f J, consumption %.2f W\n",ee->energyLevel, ee->energyMax,ee->consumption);
    }else{
      fprintf(iFile,"0.0 0.0 0.0\n");
      sprintf(buffer,"0.0 0.0 0.0\n");
    }
    fflush(iFile);
  
  
    iAdditionalInformation.clear();
    iAdditionalInformation.assign(buffer);
    Unlock();
  }

}
//*****************************************************************************

std::string EnergyLogger::GetName(){
  return "Energy Logger";
}
//*****************************************************************************

bool EnergyLogger::GetAdditionalInformation(std::string &aInformation){
  Lock();
  aInformation = iAdditionalInformation;
  Unlock();
  return true;
}

//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

